<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>工作管理</el-breadcrumb-item>
    <el-breadcrumb-item>工作列表</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card class="box-card" shadow="never">
    <template #header>
      <div class="card-header">
        <img src="../../assets/logo.svg" style="width: 50px;height: 50px;">
        <span>工作列表</span>
      </div>
    </template>
    <el-row :gutter="10">
      <el-col :span="4">
        <el-input placeholder="请输入用户名称" v-model="searchContent"></el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="handleSearch()">搜索</el-button>
        <el-button type="primary" @click="addHouseInfo()">添加</el-button>
      </el-col>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="编号" width="180">
        <template #default="scope">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.ID }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作名称" width="180">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top">
            <template #reference>
              <div class="name-wrapper">
                <el-tag size="medium">{{ scope.row.work_name }}</el-tag>
              </div>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="人数" width="180">
        <template #default="scope">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.amount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="金钱" width="180">
        <template #default="scope">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" width="180">
        <template #default="scope">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.describe }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" size="mini" @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button
          >
          <el-button
              size="mini"
              type="danger" :icon="Delete"
              @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-pagination
      v-model:currentPage="currentPage"
      :page-sizes="[3, 5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="SizeChange"
      @current-change="CurrentChange"
  >
  </el-pagination>
  <AddFamily :dialogVisible="dialogVisible" @onCloseDialog="closeDialogVisible"></AddFamily>
  <EditProduct v-if="editProductVisible === true" :editProductVisible="editProductVisible" :workID="workID" @onCloseDialog="closeEditDialog"></EditProduct>
</template>

<script>
import {reactive, onMounted, toRefs} from "vue";
import {
  Search,
  Edit,
  Delete,
} from '@element-plus/icons-vue'
import axios from "axios";
import {ElMessageBox, ElMessage} from "element-plus";
import AddFamily from "../components/AddWork.vue";
import EditProduct from "../components/EditWork.vue"
function loaData(state){
  const params = {
    page:state.currentPage,
    limit:state.pageSize,
    search_content:state.searchContent
  }
  axios.get("/api/works", {params},).then((res) => {
    state.tableData = res.data.data.res.data
    state.total = res.data.data.res.total //总记录数
  })
}
//监听pageSize改变的事件
function useHandleSizeChange(state){
  const SizeChange = (pageSize) => {
    console.log(pageSize)
    state.pageSize = pageSize
    loaData(state)
  }
  return {
    SizeChange
  }
}
//监听当前页面切换的事件
function useHandleCurrentPage(state){
  const CurrentChange = (page) => {
    state.currentPage = page
    loaData(state)
  }
  return {
    CurrentChange,
  }
}
//删除家庭数据
function houseDelete(state){
  const handleDelete = (index, row) => {
    // console.log("index = " + index + "row" + row  )
    ElMessageBox.confirm('你确定要删除该记录吗？','提示',{
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: 'warning',
      callback: (action) => {
        if(action==='confirm'){
          axios.delete("/api/work", {params:{ID:row.ID}}).then((res) => {
            if(res.data.code === 20000){
              ElMessage({
                title: "成功",
                message: "删除成功",
                type: 'success',
                duration: 2000,
              })
              loaData(state)
            }else {
              ElMessage({
                title: "失败",
                message: "删除失败",
                type: 'error',
                duration: 2000,
              })
              loaData(state)
            }

          })
          console.log("id = ", row.ID);
        }else {

        }
      }
    })
  };
  return {
    handleDelete,
  }
}
//搜索家庭数据
function useHandleSearch(state){
  const handleSearch = () => {
    state.currentPage = 1;
    const params = {
      page: state.currentPage,
      limit: state.pageSize,
      search_content: state.searchContent
    };
    axios.get("/api/works",{params}).then((res) => {
      state.tableData = res.data.data.res.data
      state.total = res.data.data.res.total //总记录数
    })
  };
  return {handleSearch}
}
//编辑访问数据
function houseHandleEdit(state){
  const handleEdit = (index, row) => {
    console.log("edit" + state.editProductVisible)
    state.editProductVisible = true
    state.workID = row.ID
  }
  return {handleEdit}
}
export default {
  components: {
    AddFamily,
    EditProduct,
  },
  setup() {
    const state = reactive({
      tableData: [],
      dialogVisible: false,//控制新增的窗口
      editProductVisible: false,//控制编辑的窗口
      total: 0,
      currentPage: 1,
      pageSize: 3,
      searchContent: "",//模糊搜索的内容
      workID:0,//编辑的商品编号
    });
    const url = import.meta.env.VITE_APP_URL
    onMounted(() => {
      loaData(state)
    });
    const addHouseInfo = () => {
      state.dialogVisible=true
    };
    const closeDialogVisible = (visible,data) => {
      state.dialogVisible = visible
      if(data !== undefined){
        loaData(state)
      }
    };
    //页码切换的时候执行该方法
    const changePage = (val) => {
      state.currentPage = val;
      loaData(state)
    }
    const closeEditDialog = (visible, res) => {
      state.editProductVisible=visible
      if(res !== undefined && res.data.code === 20000){
        loaData(state)
      }
    }
    return {
      ...toRefs(state),
      url,
      addHouseInfo,
      closeDialogVisible,
      Delete,
      Edit,
      ...houseDelete(state),
      ...useHandleSearch(state),
      ...houseHandleEdit(state),
      closeEditDialog,
      ...useHandleSizeChange(state),
      ...useHandleCurrentPage(state),
    }
  }
}
</script>

<style scoped>

</style>